package com.one.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.one.bean.ConsigneeInfo;
import com.one.bean.User;
import com.one.bean.UserLogin;
import com.one.mapper.ConsigneeMapper;
import com.one.mapper.UserLoginMapper;
import com.one.mapper.UserMapper;
import com.one.service.UserInfoService;
import com.one.vo.UserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Author ShiWen
 * @date 2022/1/19 12:29
 * @Version 1.0
 * @Description TODO
 */
@Service
public class UserInfoServiceImpl implements UserInfoService {
    @Autowired
    private ConsigneeMapper consigneeMapper;
    @Autowired
    private UserLoginMapper userLoginMapper;
    @Autowired
    private UserMapper userMapper;
    @Override
    public boolean updatePwd(String phone, String pwd) {
        LambdaQueryWrapper<UserLogin> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(UserLogin::getPhone,phone);
        UserLogin userLogin = new UserLogin();
        userLogin.setPassword(pwd);
        return userLoginMapper.update(userLogin,queryWrapper)>0;
    }

    @Override
    public boolean updateUser(User user) {

        user.setIntegral(null);
        user.setMember(null);
        user.setMagicBean(null);
        user.setUpdateDate(new Date());
        return userMapper.updateById(user) > 0;
    }

    @Override
    public UserVo selectUser(Integer id) {
        User user = userMapper.selectById(id);
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(user,userVo);
        userVo.setUserId(user.getId());
        return userVo;
    }

    @Override
    public boolean insertConsignee(ConsigneeInfo consigneeInfo) {

        return consigneeMapper.insert(consigneeInfo) > 0;
    }

    @Override
    public boolean delConsignee(Integer consigneeId) {

        return consigneeMapper.deleteById(consigneeId) > 0;
    }

    @Transactional
    @Override
    public boolean setDefault(Integer consigneeId, Integer userId) {
        // 首先将该用户的默认地址取消
        LambdaQueryWrapper<ConsigneeInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ConsigneeInfo::getUserId,userId);
        queryWrapper.eq(ConsigneeInfo::getIsdefault,true);
        ConsigneeInfo consigneeInfo = new ConsigneeInfo();
        consigneeInfo.setIsdefault(false);
        int update = consigneeMapper.update(consigneeInfo, queryWrapper);
        if (update<0){
            return false;
        }
        consigneeInfo.setId(consigneeId);
        consigneeInfo.setIsdefault(true);

        return consigneeMapper.updateById(consigneeInfo) > 0;
    }

    @Override
    public List<ConsigneeInfo> queryAllConsigneeInfo(Integer userId) {
        // 首先将该用户的默认地址取消
        LambdaQueryWrapper<ConsigneeInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ConsigneeInfo::getUserId,userId);
        return consigneeMapper.selectList(queryWrapper);
    }

    @Override
    public ConsigneeInfo queryConsigneeInfoByDefault(Integer userId) {
        // 首先将该用户的默认地址取消
        LambdaQueryWrapper<ConsigneeInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ConsigneeInfo::getUserId,userId);
        queryWrapper.eq(ConsigneeInfo::getIsdefault,true);

        return consigneeMapper.selectOne(queryWrapper);
    }
}
